/**
 * Copyright (C), 2015, 上海赛可电子商务有限公司
 * Author:   李加良
 * Date:     2015-5-6
 * Description: 详情页交互事件
 */

/**注销登录*/
function userSsoLogout(){
	$.ajax({
		url:getRootPath()+'simple/logout.htm',
		type:'post',
		dataType:'json',
		success:function(data){
			if(data){
				window.location.reload(true);
			}else{
				alert("注销失败");
				window.location.reload(true);
			}
		}
	});
}
;(function($){
	window.needProduceObj ;
	
//	/**快速注册登录成功后的回调函数*/
//	function backCallFun(obj){
//		//注册登录成功后的后续操作
//		if(backCallName=='goPreOrder'){
//			goPreOrderDetail();
//		}else if(backCallName=='goAddCollection'){
//			goAddCollectionDetail();
//		}else if(backCallName=='goCancelCollection'){
//			goCancelCollectionDetail();
//		}else{
//			window.location.reload(true);
//		}
//	}
	//立即下定触发click
	$(document).on("click",".submit_preorder",function() {
		goPreOrder(this);
    });
	//选定颜色定触发click并将属性值增加到下定标签
	$(document).on("click",".color_choose",function() {
		var colorId=$(this).attr("colorId");
		var detailId=$(this).attr("detailId");
		$("#submit_preorder").attr("colorId",colorId);
		$("#submit_preorder").attr("detailId",detailId);
    });
	//add_Collection触发click
	$(document).on("click",".add_Collection",function() {
		backCallName = 'goAddCollection';
    	checkLogin(MALL.accountQuickBase,'B');
    });
	//cancle_Collection触发click
	$(document).on("click",".cancle_Collection",function() {
		backCallName = 'goCancelCollection';
    	checkLogin(MALL.accountQuickBase,'B');
    });
	/**绑定当前点击对象为全局变量，调用快速注册登录函数*/
	function goPreOrder(obj){
		needProduceObj = obj;
    	backCallName = 'goPreOrder';
    	checkLogin(MALL.accountQuickBase,'B');
    }
	
	function goAddCollectionDetail(){
		//0：收藏成功;1：已经收藏 ; 2 收藏失败
		$.ajax({
			url:getRootPath()+"/product/addCollection.htm",
			type: "POST",
			data:{
				spuId:$("#submit_preorder").attr("spuId")
    		},
			dataType: "json",
			success: function(data){
				if(data==0){
					alert("收藏成功");
					window.location.reload(true);
				}else if(data==1){
					alert("已经收藏 ");
					window.location.reload(true);
				}else{
					alert("收藏失败 ");
				}
            }
		});
	}
	function goCancelCollectionDetail(){
		$.ajax({
			url:getRootPath()+"/product/deleteCollection.htm",
			type: "POST",
			data:{
				spuId:$("#submit_preorder").attr("spuId")
    		},
			dataType: "json",
			success: function(data){
				if(data){
					alert("取消收藏成功");
					window.location.reload(true);
				}else{
					alert("取消收藏失败");
				}
            }
		});
	}
	function goPreOrderDetail(){
    	var $this = $(needProduceObj);
		$.ajax({
			url:getRootPath()+"/preOrder/goSubscribe.htm",
			type: "POST",
			data:{
				spuId:$this.attr("spuId"),
				colorId:$this.attr("colorId"),
				detailId:$this.attr("detailId"),
				financeId:$this.attr("financeId"),
				storeId:$this.attr("storeId"),
				stockId:$this.attr("stockId")
    		},
			dataType: "json",
			success: function(data){
				if(data.parameterReturn==3){
					//跳转预定页
					window.location.href = data.url;
				}else if(data.parameterReturn==0){
					if(data.subscribeResult&&data.subscribeResult.resultCode==2){
						alert("重复报名");
					}else if(data.subscribeResult&&data.subscribeResult.resultCode==3){
						alert("商品卖完");
					}else if(data.subscribeResult&&data.subscribeResult.resultCode==4){
						alert("未开始");
					}else if(data.subscribeResult&&data.subscribeResult.resultCode==5){
						alert("已结束");
					}else if(data.subscribeResult&&data.subscribeResult.resultCode==6){
						alert("失败");
					}else if(data.subscribeResult&&data.subscribeResult.resultCode==8){
						addHasOrderTips();
					}
				}else if(data==1){
					alert("用户未登录或者未绑定手机号");
				}else if(data==2){
					alert("参数错误，不符合标准");
				}else{
					alert("服务异常");
				}
            }
		});
    }
	/**
	 * [loadData description 动态加载活动数据]
	 * @param  {[type]} activeIds  [需要加载的活动的id]
	 * @param  {[type]} showContet [请求得到的活动，需要展示的位置]
	 * @return {[type]}            [description]
	 */
	//
	function goPage(currentPageNo){
		$.ajax({
			url:getRootPath()+"/product/Evaluation.htm",
			type:'POST',
			data:{
				currentPage:currentPageNo,
				velModelId:velModelId
			},
			success:function(response){

				$('#evaluation_content').html(response);
			},
			erroe:function(errorMessage){

			}

		});
	}
	function addHasOrderTips(){
		ECar.easyDialog.open({
            container : {
            	header : '提示',
                content : '<div class="tips-hasorder">尊敬的客户，由于您已经定购了该款车辆，请完成支付或者<br />取消定单后再次购买，给您带来的不便还请谅解。<a class="button-blue" href="/member/myOrders.htm">查看我的定单</a></div>'
            }
        });
	}
	function getRootPath(){
	    var strFullPath=window.document.location.href;
	    var strPath=window.document.location.pathname;
	    var pos=strFullPath.indexOf(strPath);
	    var prePath=strFullPath.substring(0,pos);
	    var postPath=strPath.substring(0,strPath.substr(1).indexOf('/')+1);
	    if(postPath=="/saic-ebiz-carmall-web"){
	    	return(prePath+postPath);
	    }else{
	    	return prePath;
	    }
	   }
	window.goPreOrderDetail = goPreOrderDetail;
	window.goAddCollectionDetail = goAddCollectionDetail;
	window.goCancelCollectionDetail = goCancelCollectionDetail;
	
})(jQuery,window);